package com.avic.map.service.impl;

import com.avic.map.common.constant.LocalResourceConstant;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.ex.BizException;
import com.avic.map.dal.condition.PageQueryCarResourceListCondition;
import com.avic.map.dal.condition.PageQueryMapCarCondition;
import com.avic.map.dal.model.CarResource;
import com.avic.map.service.CarResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: huangsheng
 * @date: 2018/9/4
 */
@Service
@Slf4j
public class CarResourceServiceImpl extends AbstractServiceImpl implements CarResourceService {

    /**
     * 分页查询列表
     *
     * @see CarResourceService#itemList(PageQueryCarResourceListCondition)
     */
    @Override
    public List<CarResource> itemList(PageQueryCarResourceListCondition pageQueryCarResourceListCondition) {
        return carResourceMapper.itemList(pageQueryCarResourceListCondition);
    }

    /**
     * 查询数据总数
     *
     * @see CarResourceService#queryRecordsTotal(PageQueryCarResourceListCondition)
     */
    @Override
    public int queryRecordsTotal(PageQueryCarResourceListCondition pageQueryCarResourceListCondition) {
        return carResourceMapper.queryRecordsTotal(pageQueryCarResourceListCondition);
    }

    /**
     * 新增车辆资源
     *
     * @see CarResourceService#create(CarResource)
     */
    @Override
    public int create(CarResource carResource) {
        AssertUtil.isTrue(carResourceMapper.queryItemCountByCarNumber(carResource.getCarNumber()) <= 0,
                BizErrorCode.PARAM_ERROR, "车辆编号【" + carResource.getCarNumber() + "】已经存在");
        try {
            carResourceMapper.insert(carResource);
            log.info("创建车辆资源 {}", carResource.getId());
            return carResource.getId();
        } catch (DuplicateKeyException de) {
            log.warn("并发创建车辆资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "车辆资源车辆编号【" + carResource.getCarNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("创建车辆资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "创建车辆资源发生错误");
        }
    }

    /**
     * 修改车辆资源
     *
     * @see CarResourceService#update(CarResource)
     */
    @Override
    public void update(CarResource carResource) {
        log.info("修改车辆资源 {}", carResource.getId());

        CarResource dbCarResource = carResourceMapper.queryItemById(carResource.getId());
        AssertUtil.isTrue(null != dbCarResource, BizErrorCode.PARAM_ERROR, "不存在此数据");

        if (!StringUtils.equals(dbCarResource.getCarNumber(), carResource.getCarNumber())) {
            AssertUtil.isTrue(carResourceMapper.queryItemCountByCarNumber(carResource.getCarNumber()) <= 0,
                    BizErrorCode.PARAM_ERROR, "车辆编号【" + carResource.getCarNumber() + "】已经存在");
        }
        try {
            carResourceMapper.update(carResource);
        } catch (DuplicateKeyException de) {
            log.warn("并发修改车辆资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "车辆编号【" + carResource.getCarNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("修改资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "修改车辆资源发生错误");
        }
    }

    /**
     * 删除单兵资源
     *
     * @see CarResourceService#delete(int)
     */
    @Override
    public void delete(int id) {
        try {
            CarResource dbCarResource = carResourceMapper.queryItemById(id);
            if (null != dbCarResource) {
                LocalResourceConstant.CAR_STATE_DB_MAP.remove(dbCarResource.getCarNumber());
            }

            log.info("删除车辆资源 {}", id);
            carResourceMapper.delete(id);
        } catch (Exception e) {
            log.error("删除车辆资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除车辆资源的时候发生错误");
        }
    }

    @Override
    public void updateCarState(int carState, String carNumber) {
        try {
            carResourceMapper.updateCarState(carState, carNumber);
        } catch (Exception e) {
            log.error("更新车辆数据状态时发生错误 {}", e.getMessage());
        }
    }

    @Override
    public List<CarResource> mapCarRecords(PageQueryMapCarCondition pageQueryMapCarCondition) {
        return carResourceMapper.mapCarRecords(pageQueryMapCarCondition);
    }

    @Override
    public int mapCarRecordsTotal(PageQueryMapCarCondition pageQueryMapCarCondition) {
        return carResourceMapper.mapCarRecordsTotal(pageQueryMapCarCondition);
    }

    @Override
    public List<CarResource> queryCarTreeRecords() {
        return carResourceMapper.queryCarTreeRecords();
    }

    @Override
    public void updateCarImagePath(String carImagePath, int carId) {
        try {
            carResourceMapper.updateCarImagePath(carImagePath, carId);
        } catch (Exception e) {
            log.error("更新车辆图片信息时发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "更新车辆图片信息时发生错误");
        }
    }

    @Override
    public CarResource queryCarResource(int id) {
        return carResourceMapper.queryItemById(id);
    }
}
